package com.chenjim.daemon.service;

import android.app.Notification;
import android.app.Service;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Build;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import com.chenjim.daemon.IMyAidlInterface;
import com.chenjim.daemon.model.FrondServiceNotification;


public class LocalFrondService extends Service {
    private MyBinder myBinder;

    class MyBinder extends IMyAidlInterface.Stub {

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat,
                               double aDouble, String aString) throws RemoteException {

        }
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return myBinder;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d("LocalFrondService", "onCreate");
        myBinder = new MyBinder();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            FrondServiceNotification.start(this);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
            //如果 18 以上的设备 启动一个Service startForeground给相同的id
            //然后结束那个Service
            startForeground(10, new Notification());
            startService(new Intent(this, InnnerService.class));
        } else {
            startForeground(10, new Notification());
        }
    }

    @Override
    public void onDestroy() {
        Log.d("LocalFrondService", "onDestroy");
        super.onDestroy();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d("LocalFrondService", "onStartCommand,bindService");
        bindService(new Intent(this, RemoteService.class), connection, BIND_AUTO_CREATE);
        return super.onStartCommand(intent, flags, startId);
    }

    private ServiceConnection connection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            Log.d("LocalFrondService", "onServiceConnected:" + name);
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {
            Log.d("LocalFrondService", "onServiceDisconnected:" + name);
            startService(new Intent(LocalFrondService.this, RemoteService.class));
            bindService(new Intent(LocalFrondService.this, RemoteService.class), this,
                    BIND_AUTO_CREATE);
        }

        @Override
        public void onBindingDied(ComponentName name) {
            unbindService(this);
        }

        @Override
        public void onNullBinding(ComponentName name) {
            unbindService(this);
        }
    };

    public static class InnnerService extends Service {

        @Override
        public void onCreate() {
            super.onCreate();
            startForeground(10, new Notification());
            stopSelf();
        }

        @Nullable
        @Override
        public IBinder onBind(Intent intent) {
            return null;
        }
    }
}
